import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    // 存储用户数据
    userInfo: [],
    // 存储商家信息
    shopLis: [],
    // 获取商家评价
    shopRatings: [],
    // 获取商家商品数组
    shopGoods: [],
    // 购物车商品
    cartGoods: [],
    isShow: false,
    // 搜索商品的列表
    searchShops: []
  },
  mutations: {
    addUsers(state, data) {
      state.userInfo = data
    },
    logOutUser(state) {
      state.userInfo = []
    },
    // 获取商家信息
    getGoodsInfo(state, data) {
      state.shopLis = data
    },
    // 获取评论信息
    getGoodsRatings(state, data) {
      state.shopRatings = data
    },
    // 获取商品数组
    getGoods(state, data) {
      state.shopGoods = data
    },
    // 添加商品数量
    addShopGoods(state, food) {
      if (!food.count) {
        // food.count = 1// 没有属性绑定
        // 对象
        // 对象名
        // 属性名
        Vue.set(food, 'count', 1)
        // 将foods添加cartGoods
        state.cartGoods.push(food)
      } else {
        food.count++
      }
    },
    // 减少购物车商品数量
    subShopGoods(state, food) {
      if (food.count) {
        food.count--
        if (food.count === 0) {
          state.cartGoods.splice(state.cartGoods.indexOf(food), 1)
        }
      }
    },
    addUpdateFoodsNum(state, foods) {
      if (!foods.count) {
        Vue.set(foods, 'count', 1)
        state.cartGoods.push(foods)
      } else {
        foods.count++
      }
    },
    subUpdateFoodsNum(state, foods) {
      foods.count--
      if (foods.count === 0) {
        state.cartGoods.splice(state.cartGoods.indexOf(foods), 1)
      }
    },
    changeShopItems(state) {
      if (state.cartGoods.length > 0) {
        state.isShow = !state.isShow
      }
    },
    changeIsShow(state) {
      if (state.cartGoods.length === 0) {
        state.isShow = false
      }
    },
    // 清空购物车
    clearItems(state) {
      state.cartGoods.forEach(v => {
        v.count = 0
      })
      state.cartGoods = []
      state.isShow = false
    },
    // 存储搜索商品列表数据
    showSearchShops(state, step) {
      state.searchShops = step
    }
  },
  getters: {
    // 计算商品总价
    totalPrice(state) {
      let totalPrice = 0
      state.cartGoods.forEach(v => {
        totalPrice += v.count * v.price
      })
      return totalPrice
    },
    // 计算商品总数量
    totalCount(state) {
      let totalCount = 0
      state.cartGoods.forEach(v => {
        totalCount += v.count
      })
      return totalCount
    },
    // 计算满意的评价
    satisfied(state) {
      let satisfied = 0
      state.shopRatings.forEach(v => {
        if (v.rateType === 0) {
          satisfied += 1
        } else {
          satisfied += 0
        }
      })
      return satisfied
    }
  },
  actions: {
    // 异步存储用户数据
    addUsersAsync(context, step) {
      context.commit('addUsers', step)
    },
    // 异步获取用户登录信息
    logOutAsync(context) {
      context.commit('logOutUser')
    },
    // 异步获取商家列表信息
    getShopLisAsync(context, step) {
      context.commit('getGoodsInfo', step)
    },
    // 异步获取商品数组
    getGoodsAsync(context, step) {
      context.commit('getGoods', step)
    },
    // 异步获取评价列表
    getGoodsRatingsAsync(context, step) {
      context.commit('getGoodsRatings', step)
    },
    // 同步更新购物车数量
    addFoodsNum(context, food) {
      context.commit('addShopGoods', food)
    },
    // 减少商品数量
    subFoodsNum(context, food) {
      context.commit('subShopGoods', food)
    },
    // 更新foods预览页面购物车的商品数量
    updateFoodsNum(context, { isAdd, foods }) {
      if (isAdd) {
        // 添加
        context.commit('addUpdateFoodsNum', foods)
      } else {
        // 减少
        context.commit('subUpdateFoodsNum', foods)
      }
    },
    // 更新搜索商品列表数据
    getSearchShopAsync(context, searchShops) {
      context.commit('showSearchShops', searchShops)
    }
  }
})
